local jiaohua = fk.CreateSkill {
  name = "lingling__jiaohua",
}

Fk:loadTranslationTable {
  ["lingling__jiaohua"] = "教化",
  [":lingling__jiaohua"] = "一名角色造成击杀后，你可以选择：弃置其所有手牌，不少于三张则其对你造成1点伤害；你获得死亡角色所有牌，少于三张则你回复1点体力。",
  ["lingling__jiaohua_1"] = "弃置%dest所有手牌，不少于三张则其对你造成1点伤害",
  ["lingling__jiaohua_2"] = "你获得%dest所有牌，少于三张则你回复1点体力。",
}

jiaohua:addEffect(fk.Deathed, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(jiaohua.name) and data.killer
  end,
  on_cost = function(self, event, target, player, data)
    local cards = {}
    player.room.logic:getEventsOfScope(GameEvent.MoveCards, 99, function(e)
      for _, move in ipairs(e.data) do
        if move.from and move.from.dead and move.toArea == Card.DiscardPile and move.moveReason == fk.ReasonDiscard
            and move.skillName == "game_rule" then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerEquip or info.fromArea == Card.PlayerHand then
              table.insertIfNeed(cards, info.cardId)
            end
          end
        end
      end
      return e.parent == player.room.logic:getCurrentEvent()
    end, Player.HistoryPhase)
    local choices = {}
    local choice_1 = "lingling__jiaohua_1::" .. data.killer.id
    local choice_2 = "lingling__jiaohua_2::" .. data.who.id
    if not (data.killer.dead or data.killer:isKongcheng()) then
      table.insert(choices, choice_1)
    end
    if #cards > 0 then
      table.insert(choices, choice_2)
    end
    if #choices == 0 then return end
    table.insert(choices, "Cancel")
    local choice = player.room:askToChoice(player, {
      choices = choices,
      all_choices = { choice_1, choice_2, "Cancel" },
      skill_name = jiaohua.name,
    })
    if choice ~= "Cancel" then
      if choice == choice_1 then
        event:setCostData(self, { choice = 1, cards = cards })
      else
        event:setCostData(self, { choice = 2, cards = cards })
      end
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self).cards
    if player.dead then return end
    local choice = event:getCostData(self).choice
    if choice == 1 then
      local cids = data.killer:getCardIds("h")
      room:throwCard(cids, jiaohua.name, data.killer, player)
      if #cids >= 3 and not (data.killer.dead or player.dead) then
        room:damage {
          to = player,
          from = data.killer,
          damage = 1,
          skillName = jiaohua.name,
        }
      end
    elseif choice == 2 then
      room:moveCardTo(cards, Player.Hand, player, fk.ReasonJustMove, jiaohua.name, nil, true, player)
      if #cards < 3 and not player.dead then
        room:recover {
          who = player,
          num = 1,
          skillName = jiaohua.name,
        }
      end
    end
  end,
})

return jiaohua
